const EventEmitter = require('events');

const emitter = new EventEmitter();

emitter.on('saved', () => {
    console.log(`A saved event occurred.`);
});

emitter.emit('saved');


// -----------------
const EventEmitter2 = require('events');

const emitter2 = new EventEmitter2();

emitter2.on('saved', (arg) => {
    console.log(`A saved event occurred: name: ${arg.name}, id: ${arg.id}`);
});

emitter2.emit('saved', {
    id: 1,
    name: 'John Doe'
});

// -----------------
const EventEmitter3 = require('events');

const emitter3 = new EventEmitter3();

// declare the event handler
function log(arg) {
    console.log(`A saved event occurred, name: ${arg.name}, id: ${arg.id}`);
}

// attach the event listener to the saved event
emitter3.on('saved', log);

// emit the saved event
emitter3.emit('saved', {
    id: 1,
    name: 'John Doe'
});

// remove the event listener
emitter3.off('saved', log);

// no effect
emitter3.emit('saved', {
    id: 2,
    name: 'Jane Doe'
});